{extend name="public:base"}
{block name='css'}
{/block}

{block name='body'}
<div class="view view-main white-navbar white-toolbar">
    <div data-name="home" class="page">

        <div class="navbar">
            <div class="navbar-inner">
                <div class="left"><a href="{$my_prev_url|default='/'}" class="external"><i class="iconfont icon-fanhui"></i></a></div>
                <div class="center"><span>购物车</span></div>
                <div class="right"></div>
            </div>
        </div>

        <div class="page-content  padding-bottom50">
            <form name="form" action="" id="form"  method="post" >
                <div class="car-goods">
                    {empty name="list"}
                    <div class="no-data" style="display: block;"><i class="iconfont icon-jigou_wushuju"></i><br />无任何数据...</div>
                    {else/}
                    <div class="no-data"><i class="iconfont icon-jigou_wushuju"></i><br />无任何数据...</div>
                    <ul class="u-list">
                        {volist name='list' id='vo'}
                        <li data-id="{$vo.id}" data-price="{$vo.price}">
                            <label class="checkbox color-green" onclick="radios(this);return false;"><input type="checkbox" checked="{$vo.checked}"><i class="icon-checkbox"></i></label>
                            <div class="y1">
                                <img src="{$vo.img}" alt="" />
                                <div class="y2">
                                    <div class="t1">
                                        <h2>{$vo.title}</h2>
                                        <a href="javascript:;" onclick="delGoods(this);"><i class="iconfont icon-shanchu"></i></a>
                                    </div>
                                    {notempty name='vo.spec'}
                                    <div class="t1">
                                        <h2 style="font-size: 12px;margin-top: 3px;color: #999;">{$vo.spec}</h2>
                                    </div>
                                    {/notempty}
                                    <div class="t2" style="margin-top: 5px;">
                                        <span class="z1">￥{$vo.price}</span>
                                        <div class="nm">
                                            <a href="javascript:;" onclick="jj(this,-1);">-</a>
                                            <input type="text" value="{$vo.num}" name="number" readonly="readonly">
                                            <a href="javascript:;" onclick="jj(this,1);">+</a>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </li>
                        {/volist}
                    </ul>
                    {/empty}
                </div>

            </form>
        </div>

        {notempty name="list"}
        <div class="toolbar">
            <div class="topay">
                <div class="z"><label class="abs checkbox color-green" onclick="allSelect();return false;"><input type="checkbox" class="js-select-all" ><i class="icon-checkbox"></i></label> 全选&nbsp;&nbsp;&nbsp;合计：<span class="countprice">￥0.00</span></div>
                <div class="y" onclick="subOrder();">提交订单</div>
            </div>
        </div>
        {else/}
        {include file='public:tab' /}
        {/notempty}

    </div>
</div>
{/block}

{block name="js"}
<script type="text/javascript">
    $(function(){
        countPrice();
    });
    function subOrder(){
        var onIs = false ;
        var ids = '';
        $('.car-goods li').each(function(i){
            if ( $(this).find("input[type=checkbox]").is(':checked') ) {
                onIs = true ;
                ids += $(this).attr('data-id') + ',';
            }
        });
        if ( ! onIs ) {
            messageToast("您还没选择任何商品");
        } else {
            ids = ids.substr(0, ids.length - 1);
            self.location = "{:url('place')}?cart_ids="+ids;
        }
    }


    //全选
    var allOn  = true ;
    function allSelect( ){
        allOn = allOn ? false : true ;
        $.post( "{:url('checkCartGoods')}" , {checkbox:allOn ? 'all' : 'not_all'}, function ( data ) {
            if (data.status == 'y') {
                $(".car-goods li label").each(function(){
                    $(this).find('input').prop('checked' , allOn);
                });
                countPrice();
            } else {
                messageToast( data.info );
            }
        }, "json");
    }
    /*
    function allOns(){
        allOn = true ;
        $(".car-goods li label").each(function(){
            if ( !$(this).find('input').is(':checked') ) {
                allOn = false ;
            }
        });
        $(".topay input").prop('checked' , allOn) ;
    }
    */

    //单个选中
    function radios( obj ){
        var id = $(obj).parent("li").attr('data-id') ;
        var checked = $(obj).find("input[type=checkbox]").is(':checked');
        $.post( "{:url('checkCartGoods')}" , {checkbox:checked ? 'false' : 'true',id:id}, function ( data ) {
            if (data.status == 'y') {
                $(obj).find("input[type=checkbox]").prop('checked' , !checked);
                countPrice();
            } else {
                messageToast( data.info );
            }
        }, "json");
    }

    //重新计算总价
    function countPrice(){
        var countprice = 0 ;
        var flag = true;
        $('.car-goods li').each(function(i){

            if ( $(this).find("input[type='checkbox']").is(':checked') ) {
                var price = parseFloat( $(this).attr('data-price') ) ;
                var number = parseFloat( $(this).find("input[name=number]").val() ) ;
                countprice += price * number ;
            } else {
                flag = false;
            }

        });
        allOn = flag;
        $('.js-select-all').prop('checked', allOn);
        $(".countprice").html('￥' + countprice.toFixed(2)) ;
    }

    //删除
    function delGoods(obj){
        app.dialog.confirm("您确定要删除该商品吗？", function(){
            var id = $(obj).parents("li").attr('data-id') ;
            $.post( "{:url('delCartGoods')}" , {id:id}, function ( data ) {
                if (data.status == 'y') {
                    $(obj).parents('li').remove();
                    if ($('.car-goods li').length <= 0) {
                        $('.no-data').show();
                        $('.toolbar').remove();
                    } else {
                        countPrice();
                    }
                } else {
                    messageToast(data.info);
                }
            }, "json");
        });
    }

    //数量加减
    function jj( obj, val ) {
        var number = parseInt( $(obj).parent().find('input').val() ) ;
        if ( val + number > 0 ) {
            $(obj).parent().find('input').val( val+number ) ;

            var id = $(obj).parents("li").attr('data-id') ;
            number = val + number ;
            $.post( "{:url('editCartGoodsNum')}" , {id:id , num:number}, function ( data ) {
                if (data.status == 'n') {
                    $(obj).parent().find('input').val( number ) ;
                }
            }, "json");

            countPrice();
        }
    }
</script>
{/block}